<%--
  Created by IntelliJ IDEA.
  User: cos
  Date: 2022/3/14
  Time: 16:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        .c1 {
            color: red;
        }
    </style>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://fastly.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
            integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
            crossorigin="anonymous"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>

</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
    <h1  class="text-center">这家电子文档</h1>
    <%-- <form action="/entry/add" method="post">--%>
    <form id="myForm" class="form-horizontal">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">文档名称</label>
            <div class="col-sm-10">
                <input type="text" name="title" class="form-control" id="title" placeholder="文档名称">
            </div>
        </div>
        <div class="form-group">
            <label for="summary" class="col-sm-2 control-label">文章摘要</label>
            <div class="col-sm-10">
                <input type="text" name="summary" class="form-control" id="summary" placeholder="文章摘要">
            </div>
        </div>
        <div class="form-group">
            <label for="uploaduser" class="col-sm-2 control-label">上传人</label>
            <div class="col-sm-10">
                <input type="text" name="uploaduser" class="form-control" id="uploaduser" placeholder="上传人">
            </div>
        </div>
        <div class="form-group">
            <label for="createdate" class="col-sm-2 control-label">上传时间</label>
            <div class="col-sm-10">
                <input type="date" name="createdate" class="form-control" id="createdate" placeholder="上传时间">
            </div>
        </div>
        <div class="form-group">
            <label for="createdate" class="col-sm-2 control-label">电子书分类</label>
            <div class="col-sm-10">
                <select name="categoryId" id="categoryId" class="form-control">
                </select>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" id="btn2" class="btn btn-default">提交</button>
                <button type="button" id="btn1" class="btn btn-default">返回</button>
            </div>
        </div>
    </form>
        </div></div>
</div>
<script>
    $(function () {
        $.ajax({
            type: "get",
            url: "/category/getAll",
            success: function (data) {
                for (let x in data) {
                    $("#categoryId").append("<option value='" + data[x].id + "'>" + data[x].name + "</option>")
                }
            }
        })
        $("#btn1").click(function () {
            //后退一页
            history.go(-1);
        })

        $("#btn2").click(function () {
            //获取数据
            var datas = $("#myForm").serialize();
            $.ajax({
                type: "post",
                url: "/entry/add",
                data: datas,
                success: function (data) {
                    if (data == 1) {
                        alert("新增成功");
                        location.href = "/index2.jsp";
                    } else {
                        alert("新增失败");
                    }
                }
            })
        })
    })
</script>
</body>
</html>
